static void add_disktype(blkif_t *blkif, int type)
{
- driver_list_entry_t *entry, *ptr, *last;
+ driver_list_entry_t *entry, **pprev;
- if (type > MAX_DISK_TYPES) return;
+ if (type > MAX_DISK_TYPES)
+ return;
entry = malloc(sizeof(driver_list_entry_t));
entry->blkif = blkif;
- entry->next = NULL;
- ptr = active_disks[type];
+ entry->next = NULL;
- if (ptr == NULL) {
- active_disks[type] = entry;
- entry->prev = NULL;
- return;
- }
-
- while (ptr != NULL) {
- last = ptr;
- ptr = ptr->next;
- }
+ pprev = &active_disks[type];
+ while (*pprev != NULL)
+ pprev = &(*pprev)->next;
- /*We've found the end of the list*/
- last->next = entry;
- entry->prev = last;
-
- return;
+ *pprev = entry;
+ entry->pprev = pprev;
}
static int del_disktype(blkif_t *blkif)
{
- driver_list_entry_t *ptr, *cur, *last;
+ driver_list_entry_t *entry, **pprev;
int type = blkif->drivertype, count = 0, close = 0;
- if (type > MAX_DISK_TYPES) return 1;
-
- ptr = active_disks[type];
- last = NULL;
- while (ptr != NULL) {
- count++;
- if (blkif == ptr->blkif) {
- cur = ptr;
- if (ptr->next != NULL) {
- /*There's more later in the chain*/
- if (!last) {
- /*We're first in the list*/
- active_disks[type] = ptr->next;
- ptr = ptr->next;
- ptr->prev = NULL;
- }
- else {
- /*We're sandwiched*/
- last->next = ptr->next;
- ptr = ptr->next;
- ptr->prev = last;
- }
-
- } else if (last) {
- /*There's more earlier in the chain*/
- last->next = NULL;
- } else {
- /*We're the only entry*/
- active_disks[type] = NULL;
- if(dtypes[type]->single_handler == 1)
- close = 1;
- }
- DPRINTF("DEL_DISKTYPE: Freeing entry\n");
- free(cur);
- if (dtypes[type]->single_handler == 0) close = 1;
+ if (type > MAX_DISK_TYPES)
+ return 1;
- return close;
- }
- last = ptr;
- ptr = ptr->next;
+ pprev = &active_disks[type];
+ while ((*pprev != NULL) && ((*pprev)->blkif != blkif))
+ pprev = &(*pprev)->next;
+
+ if ((entry = *pprev) == NULL) {
+ DPRINTF("DEL_DISKTYPE: No match\n");
+ return 1;
}
- DPRINTF("DEL_DISKTYPE: No match\n");
- return 1;
+
+ *pprev = entry->next;
+ if (entry->next)
+ entry->next->pprev = pprev;
+
+ DPRINTF("DEL_DISKTYPE: Freeing entry\n");
+ free(entry);
+
+ /* Caller should close() if no single controller, or list is empty. */
+ return (!dtypes[type]->single_handler || (active_disks[type] == NULL));
}
static int write_msg(int fd, int msgtype, void *ptr, void *ptr2)
if (del_disktype(blkif)) {
close(blkif->fds[WRITE]);
close(blkif->fds[READ]);
-
}
+
return 0;
}
{
tapdev_info_t *info = s->ring_info;
struct tap_disk *drv = s->drv;
- fd_list_entry_t *ptr, *prev;
+ fd_list_entry_t *entry;
drv->td_close(s);
if (info != NULL && info->mem > 0)
munmap(info->mem, getpagesize() * BLKTAP_MMAP_REGION_SIZE);
- ptr = s->fd_entry;
- prev = ptr->prev;
-
- if (prev) {
- /*There are entries earlier in the list*/
- prev->next = ptr->next;
- if (ptr->next) {
- ptr = ptr->next;
- ptr->prev = prev;
- }
- } else {
- /*We are the first entry in list*/
- if (ptr->next) {
- ptr = ptr->next;
- fd_start = ptr;
- ptr->prev = NULL;
- } else fd_start = NULL;
- }
+ entry = s->fd_entry;
+ *entry->pprev = entry->next;
+ if (entry->next)
+ entry->next->pprev = entry->pprev;
close(info->fd);
return 0;
}
-static inline fd_list_entry_t *add_fd_entry(int tap_fd, int io_fd[MAX_IOFD], struct td_state *s)
+static inline fd_list_entry_t *add_fd_entry(
+ int tap_fd, int io_fd[MAX_IOFD], struct td_state *s)
{
- fd_list_entry_t *ptr, *last, *entry;
+ fd_list_entry_t **pprev, *entry;
int i;
+
DPRINTF("Adding fd_list_entry\n");
/*Add to linked list*/
s->fd_entry = entry = malloc(sizeof(fd_list_entry_t));
entry->tap_fd = tap_fd;
- for (i = 0; i < MAX_IOFD; i++) entry->io_fd[i] = io_fd[i];
+ for (i = 0; i < MAX_IOFD; i++)
+ entry->io_fd[i] = io_fd[i];
entry->s = s;
entry->next = NULL;
- ptr = fd_start;
- if (ptr == NULL) {
- /*We are the first entry*/
- fd_start = entry;
- entry->prev = NULL;
- goto finish;
- }
+ pprev = &fd_start;
+ while (*pprev != NULL)
+ pprev = &(*pprev)->next;
- while (ptr != NULL) {
- last = ptr;
- ptr = ptr->next;
- }
- last->next = entry;
- entry->prev = last;
+ *pprev = entry;
+ entry->pprev = pprev;
- finish:
return entry;
}